package com.example.module_properties.view

import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.module_properties.R
import com.example.module_properties.adapter.ProperItemTypeAdapter
import com.example.module_properties.adapter.ProrperAdapter
import com.example.module_properties.databinding.ActivityPropetiesBinding
import com.example.module_properties.vm.ProperTiesViewModel
import com.yuwei.base.BaseActivity
import com.yuwei.common.path.RouterPath
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = RouterPath.PROP)
class PropetiesActivity : BaseActivity<ActivityPropetiesBinding, ProperTiesViewModel>() {
    private val prorperAdapter = ProrperAdapter()
    var mPosition = -1
    var mPositionType = -1
    private val properItemTypeAdapter = ProperItemTypeAdapter()
    override fun initView() {
        binding.propRv.adapter = prorperAdapter
        binding.propRv.layoutManager = LinearLayoutManager(this)
        vm.getPreopertiesList()
        prorperAdapter.setOnItemClickListener { adapter, view, position ->
            mPosition = position
            prorperAdapter.setPosition(mPosition)
            vm.getRegionPrep(prorperAdapter.data[mPosition].regionId)
            prorperAdapter.notifyDataSetChanged()
        }
        binding.propTypeRv.adapter = properItemTypeAdapter
        binding.propTypeRv.layoutManager = LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false)
        binding.propBack.setOnClickListener {
            finish()
        }
        properItemTypeAdapter.setOnItemClickListener { adapter, view, position ->
            mPositionType = position
            properItemTypeAdapter.setPosition(mPositionType)
            vm.getPreopertiesList()
            properItemTypeAdapter.notifyDataSetChanged()
        }
        vm.getProperSuccess.observe(this){
            prorperAdapter.data.clear()
            val value = it.groupBy { it.regionId }.map {
                it.value.first()
            }
            if (mPositionType!=-1){
                prorperAdapter.data.addAll(value.filter {tt->
                    tt.regionName == properItemTypeAdapter.data[mPositionType]
                })
                prorperAdapter.notifyDataSetChanged()
            }else{
                prorperAdapter.data.addAll(value)
                prorperAdapter.notifyDataSetChanged()
            }

        }

    }

    override fun initData() {
        properItemTypeAdapter.data.add("智慧园区A区")
        properItemTypeAdapter.data.add("长坂坡A区")
        properItemTypeAdapter.data.add("A区")
        properItemTypeAdapter.data.add("B区")
        properItemTypeAdapter.data.add("C区")
        properItemTypeAdapter.notifyDataSetChanged()
        vm.getRegionSuccess.observe(this){
            prorperAdapter.setList(it)
            prorperAdapter.notifyDataSetChanged()
        }

    }

}